/**
 * 报表设计公司(2011)
 */
package com.reportgear.designer.ui.base;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;

import com.reportgear.designer.ui.support.ColorChangeEvent;
import com.reportgear.designer.ui.support.ColorChangeListener;

/**
 * @version 1.0 2011-3-24
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public class JColorButton extends JButton {

	/** 选择的颜色 */
	private Color selectedColor = Color.WHITE;

	// private ListenerSupport<ColorChangeListener> listeners = new
	// ListenerSupport<ColorChangeListener>(this);

	public JColorButton() {
		super();
		this.setIcon(new ColorSwatch(this.selectedColor));
		init();
	}

	public JColorButton(String caption) {
		this();
		this.setText(caption);
	}

	/** 为按钮添加弹出颜色选择器 */
	private void init() {
		this.setRequestFocusEnabled(false);
		this.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				final JColorChooser chooser = new JColorChooser(selectedColor != null ? selectedColor : Color.WHITE);
				JDialog dialog = JColorChooser.createDialog(getParent(), "选择颜色", true, chooser, new ActionListener() {

					public void actionPerformed(ActionEvent ae) {
						setSelectedColor(chooser.getColor());
						fireColorChangeListener(new ColorChangeEvent(JColorButton.this, getSelectedColor()));
					}
				}, null);
				dialog.setVisible(true);
			}
		});
	}

	/**
	 * @return the iconColor
	 */
	public Color getSelectedColor() {
		return selectedColor;
	}

	/**
	 * @param iconColor
	 *            the iconColor to set
	 */
	public void setSelectedColor(Color selectedColor) {
		this.selectedColor = selectedColor;
		this.setIcon(new ColorSwatch(selectedColor));
	}

	public void addColorChangeListener(ColorChangeListener listener) {
		this.listenerList.add(ColorChangeListener.class, listener);
	}

	public void removeColorChangeListener(ColorChangeListener listener) {
		this.listenerList.remove(ColorChangeListener.class, listener);
	}

	public void fireColorChangeListener(ColorChangeEvent evt) {
		for (ColorChangeListener listener : this.listenerList.getListeners(ColorChangeListener.class)) {
			if (listener != null) {
				listener.changeColor(evt);
			}
		}
	}

	/** 颜色按钮的图标类 */
	private class ColorSwatch implements Icon {

		private Color color = Color.BLACK;
		private int ICON_WIDTH = 11;

		public ColorSwatch(Color color) {
			this.color = color;
		}

		@Override
		public int getIconWidth() {
			return ICON_WIDTH;
		}

		@Override
		public int getIconHeight() {
			return this.getIconWidth();
		}

		@Override
		public void paintIcon(Component c, Graphics g, int x, int y) {
			g.setColor(Color.BLACK);
			g.fillRect(x, y, getIconWidth(), getIconHeight());
			g.setColor(color);
			g.fillRect(x + 1, y + 1, getIconWidth() - 2, getIconHeight() - 2);
		}
	}
}
